<?php
namespace app\index\controller;
use think\Controller;
use app\admin\model\Type;
use app\admin\model\Goods as GoodsModel;
use app\index\model\Pic;
use app\index\model\Car;
use app\index\model\Comment;
use app\index\model\Order;
use app\index\model\Collection;

class Goods extends Controller
{
	private $typeModel;
	private $goodsModel;
	private $carModel;
	private $commentModel;
	private $orderModel;
	public function _initialize() {
        $this->typeModel = new Type();
        $this->goodsModel = new GoodsModel();
        $this->carModel = new Car();
        $this->commentModel = new Comment();
        $this->orderModel = new Order();
    }

	public function index()
	{
		$pid = input('get.id');
		// $pname = input('get.name');
		// 分类
    	$types = $this->typeModel->getNormalFirstType();
    	$childType = $this->typeModel->getChildType();
		
		// 获取分类下所有的商品
		if (is_null($pid)) {
			$allproducts = $this->goodsModel->getGoods();
		} else {
			$allproducts = $this->goodsModel->getGoodsByCategory1($pid);
		}
		
		// 当前用户的购物车数量
		$uid = session('user')['id'];
		$cars = $this->carModel->where('uid',$uid)->select();
		$total_price = $this->carModel->where('uid',$uid)->sum('price');
		$cars_num = count($cars);
		return $this->fetch('',[
			'types' => $types,
			'childType' => $childType,
			'products' => $allproducts,
			'cars' => $cars,
			'shop_num' => $cars_num,
			'total_price' => $total_price,
		]);
	}

	// 商品详情
	public function goods_detail()
	{
		$id = input('get.id');
		$goods = $this->goodsModel->get($id);
		$pic = new Pic();
		$pics = $pic->where(['gid'=>$id])->select();
		$comments = $this->commentModel->where(['gid'=>$id,'status'=>0])->select();
		$uid = session('user')['id'];
		$cars = $this->carModel->where('uid',$uid)->select();
		$cars_num = count($cars);
		return $this->fetch('',[
			'goods' => $goods,
			'pics' => $pics,
			'cars' => $cars,
			'shop_num' => $cars_num,
			'comments' => $comments,
		]);
	}

	// 通过商品类别获取商品详情
	public function getGoodsByCategory()
	{
		$types = $this->typeModel->getNormalFirstType();
    	$childType = $this->typeModel->getChildType();
    	$id = input('get.id');
    	$cid = input('get.cid');
    	if ($id) {
    		$product = $this->goodsModel->getGoodsByCategory1($id);
    	} elseif($cid) {
    		$product = $this->goodsModel->getGoodsByCategory2Id($cid);
    	}
    	$uid = session('user')['id'];
    	$cars = $this->carModel->where('uid',$uid)->select();
		$cars_num = count($cars);
    	return $this->fetch('index',[
			'types' => $types,
			'childType' => $childType,
			'products' => $product,
			'cars' => $cars,
			'shop_num' => $cars_num,
		]);
	}

	// 商品排序
	public function orderby($order,$id_type,$id)
	{
		if($id_type == 'id'){
			$data = [
				'category1_id' => $id,
				'dels' => ['neq',0],
			];
			$order = [
				$order => 'desc',
				'id' => 'desc',
			];
			
		} else if($id_type == 'cid'){
			$data = [
				'category2_id' => $id,
				'dels' => ['neq',0],
			];
			$order = [
				$order => 'desc',
				'id' => 'desc',
			];
		}
		$res = $this->goodsModel->where($data)->order($order)->select();
		if (!$res) {
			return show(0,'error');
		}
		return show(1,'success',$res);
	}

	// 加入购物车
	public function shop_car()
	{
		if (!request()->isAjax()) {
			$this->error('请求不合法');
		} else {
			$data = input('post.');
			$car = new Car();
			$res = $car->allowField(true)->save($data);
			if ($res) {
				$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
			} else {
				$this->result($_SERVER['HTTP_REFERER'], 0, '加入失败');
			}
		}	
	}

	// 删除购物车商品
	public function delete_car($id)
	{
		$uid = session('user')['id'];
		$res = $this->carModel->destroy($id);
		if ($res) {
			$cars = $this->carModel->where('uid',$uid)->select();
			return show(1,'success',$cars);
		} else {
			return show(0,'删除失败');
		}
	}
	// 用户购物车页面
	public function shopping_cart()
	{
		$user = session('user');
		if (!$user) {
			$this->redirect(url('user/login'));
		}
		$uid = $user->id;
		$cars = $this->carModel->where('uid',$uid)->select();
		return $this->fetch('',[
			'user' => $user,
			'cars' => $cars,
		]);
	}

	// 商品收藏
	public function collect($id)
	{
		$res = $this->goodsModel->get($id);
		if ($res) {
			return show(1,'success',$res);
		} else {
			return show(0,'error');
		}	
	}

	// 添加收藏
	public function collection_add()
	{
		if (!request()->isAjax()) {
			$this->error('请求不合法');
		}
		$data = input('post.');
		$res = Collection::create($data);
		if($res){
			$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
		}
		$this->result($_SERVER['HTTP_REFERER'],0,'添加失败');
	}

	// 用户评价
	public function goods_comment()
	{
		$gid = input('get.id');
		$uid = session('user')['id'];
		$comments = $this->commentModel->where(['gid'=>$gid,'status'=>0])->select();
		$goods = $this->goodsModel->get($gid);
		$commented = $this->commentModel->where(['uid'=>$uid,'gid'=>$gid])->find();
		return $this->fetch('',[
			'goods' => $goods,
			'comments' => $comments,
			'commented' => $commented
		]);
	}

	// 添加用户评价
	public function add_comment()
	{
		$data = input('post.');
		$uid = session('user')['id'];
		$data['uid'] = $uid;
		$gid = $data['gid'];
		$order_no = $this->orderModel->where('gid',$gid)->column('order_no');
		$this->orderModel->save(['status'=>5],['order_no'=>$order_no[0]]);
		$res = $this->commentModel->isUpdate(false)->save($data);
		if($res){
			$this->result($_SERVER['HTTP_REFERER'], 1, 'success');
		}
		$this->result($_SERVER['HTTP_REFERER'],0,'评论失败');
	}

}